<template>
	<xn-form-container
		:title="formData.id ? '编辑产品分类' : '增加产品分类'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="上级分类：" name="parentId">
				<a-tree-select
					v-model:value="formData.parentId"
					class="xn-wd"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择上级分类"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					:field-names="{
						children: 'children',
						label: 'name',
						value: 'id'
					}"
					selectable="false"
					tree-line
				/>
			</a-form-item>
			<a-form-item label="分类编码：" name="categoriesCode">
				<a-input v-model:value="formData.categoriesCode" placeholder="请输入分类编码" allow-clear />
			</a-form-item>
			<a-form-item label="分类名称：" name="categoriesName">
				<a-input v-model:value="formData.categoriesName" placeholder="请输入分类名称" allow-clear />
			</a-form-item>
			<a-form-item label="分类图片：" name="categoriesImage">
				<xn-upload v-model:value="formData.categoriesImage" uploadMode="image" />
			</a-form-item>
			<a-form-item label="是否销售时隐藏：" name="categoriesHideforsale">
				<a-radio-group v-model:value="formData.categoriesHideforsale" placeholder="请选择是否销售时隐藏" :options="categoriesHideforsaleOptions" />
			</a-form-item>
			<a-form-item label="排序码：" name="sortCode">
				<a-input v-model:value="formData.sortCode" placeholder="请输入排序码" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="supplierCategoriesForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import supplierCategoriesApi from '@/api/supplier/base/supplierCategoriesApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	    const categoriesHideforsaleOptions = ref([])
	// 定义上级分类数据
	const treeData = ref([])
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		nextTick(() => {
			// 获取机构树并加入顶级
			supplierCategoriesApi.categoriesTreeSelector().then((res) => {
				treeData.value = [
					{
						id: 0,
						parentId: '-1',
						name: '全部分类',
						children: res
					}
				]
			})
		});
		categoriesHideforsaleOptions.value = tool.dictList('MENU_VISIBLE')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		parentId: [required('请输入父ID')],
		categoriesCode: [required('请输入分类编码')],
		categoriesName: [required('请输入分类名称')],
		categoriesHideforsale: [required('请输入是否销售时隐藏')],
		sortCode: [required('请输入排序码')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				supplierCategoriesApi
					.supplierCategoriesSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
